package com.lin.game.utils;

import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//管理忘记密码
public class MapCodeUtils {
    private static Map<String, List> codeMap=new ConcurrentHashMap<>();//线程安全的Map
    public static void putCode(String key, String code){
        List list=new ArrayList();
        list.add(code);//验证码
        list.add(System.currentTimeMillis());//当前时间 毫秒值
        codeMap.put(key,list);
    }
    //获得验证码
    public static String getCode(String key){
        if (codeMap.containsKey(key)){
            codeMap.get(key);
            List valist=codeMap.get(key);
            String code = (String) valist.get(0);
            long setTime = (long) valist.get(1);
            long nowTime = System.currentTimeMillis();

            if(nowTime-setTime>5*60*1000){
                codeMap.remove(key);
                //时效过了
                return null;
            }
            return code;
        }
        return null;
    }
    public static void removeCode(String key){
        codeMap.remove(key);
    }


}
